package booyah.web;

import booyah.application.Event;
import booyah.application.User;
import booyah.service.EventService;
import booyah.validation.EventValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Controller
public class EventController {

    private final EventService eventService;

    @Autowired
    public EventController(EventService eventService) {
        this.eventService = eventService;
    }

    @ModelAttribute
    public Event newRequest(@RequestParam(required = false) Long id) {
        return (id != null) ? eventService.findEventByEventId(id) : new Event();
    }

    @InitBinder
    public void setAllowedFields(WebDataBinder dataBinder) {
        TimestampEditor timestampEditor = new TimestampEditor();
        dataBinder.registerCustomEditor(Timestamp.class, timestampEditor);
    }

    @RequestMapping(method = RequestMethod.GET)
    public Collection<Event> myEvents(HttpSession session) {
        User user = (User) session.getAttribute("user");
        return eventService.findEventsForAdministrator(user);
    }

    @RequestMapping(method = RequestMethod.GET)
    public void show() {
    }

    @RequestMapping(method = RequestMethod.GET)
    public void details() {
    }

    @RequestMapping(method = RequestMethod.POST)
    public String show(@ModelAttribute Event event) {
        return "redirect:/controller/home";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String futureEvents(HttpSession session, Model model) {
        List<Event> events = eventService.findEventsByStartDate(new Timestamp(new Date().getTime()));
        model.addAttribute("eventList", events);
        return "controller/event/list";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String allEvents(HttpSession session, Model model) {
        List<Event> events = eventService.findAllEvents();
        model.addAttribute("eventList", events);
        return "controller/event/list";
    }

    @RequestMapping(method = RequestMethod.GET)
    public Model form(HttpSession session, Model model) {
        model.addAttribute("sessionUser", session.getAttribute("user"));
        Event event = (Event) model.asMap().get("event");
        return model;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String form(@ModelAttribute Event event, BindingResult result, SessionStatus status, HttpSession session) {

        EventValidator.validate(event, result);
        if (result.hasErrors()) {
            return null; // Default view
        } else {
            if (event.getAdministrator() == null) {
                event.setAdministrator((User) session.getAttribute("user"));
            }
            eventService.updateEvent(event);
            status.setComplete();
            return "redirect:/controller/event/myEvents";
        }
    }

    @RequestMapping(method = RequestMethod.GET)
    public String delete(@ModelAttribute Event event, HttpSession session) {
        eventService.deleteEvent(event);
        return "redirect:/controller/event/myEvents";
    }

    @RequestMapping(method = RequestMethod.GET)
    public void list(Model model) {
//        model.addAttribute("eventList", eventService.findAllEvents());
    }

}
